package com.tanhua.manage.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.tanhua.manage.ManageApplication;
import com.tanhua.manage.enums.CityEnum;
import com.tanhua.manage.enums.SexEnum;
import com.tanhua.manage.mapper.UserInfoMapper;
import com.tanhua.manage.pojo.*;
import com.tanhua.manage.vo.UserDistributionVo;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import java.util.*;


@SpringBootTest(classes = ManageApplication.class)
@RunWith(SpringJUnit4ClassRunner.class)
public class testJun {

    @Autowired
    private UserInfoMapper userInfoMapper;

    private static final ObjectMapper MAPPER = new ObjectMapper();

    @Test
    public void test() throws JsonProcessingException {

        QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.select();
        List<UserInfo> list = this.userInfoMapper.selectList(queryWrapper);

        Date date1 = new Date();
        Date date2 = new Date();

        Set<String> set1  = new HashSet<>();
        Set<SexEnum> set2  = new HashSet<>();
        Set<String> set3  = new HashSet<>();

        UserDistributionVo distributionVo = new UserDistributionVo();

        List<IndustryDistribution> industryDistributions = new ArrayList<>();
        List<LocalDistribution> localDistributions = new ArrayList<>();
        List<GenderDistribution> genderDistributions = new ArrayList<>();
        List<AgeDistribution> ageDistributions = new ArrayList<>();
        List<LocalTotal> localTotals = new ArrayList<>();


        for (UserInfo userInfo : list) {
            String industry = userInfo.getIndustry();
            SexEnum sex = userInfo.getSex();
            String userCity = userInfo.getCity();
            String city = userCity.substring(0, 2);

            set1.add(industry);
            set2.add(sex);
            set3.add(city);
        }

        for (String industry : set1) {
            QueryWrapper<UserInfo> queryWrapper1 = new QueryWrapper<>();
            IndustryDistribution industryDistribution = new IndustryDistribution();

            industryDistribution.setTitle(industry);
            queryWrapper1.gt("created", date1).lt("created", date2).like("industry", industry);
            industryDistribution.setAmount(this.userInfoMapper.selectCount(queryWrapper1));

            industryDistributions.add(industryDistribution);
        }

        getAge1(date1, date2, ageDistributions);
        getAge2(date1, date2, ageDistributions);
        getAge3(date1, date2, ageDistributions);
        getAge4(date1, date2, ageDistributions);
        getAge5(date1, date2, ageDistributions);
        getAge6(date1, date2, ageDistributions);


        for (SexEnum sex : set2) {
            if(sex.getValue() != 3){
                QueryWrapper<UserInfo> queryWrapper1 = new QueryWrapper<>();
                GenderDistribution genderDistribution = new GenderDistribution();

                genderDistribution.setTitle(sex.toString());
                queryWrapper1.gt("created", date1).lt("created", date2).like("sex", sex.getValue());
                genderDistribution.setAmount(this.userInfoMapper.selectCount(queryWrapper1));

                genderDistributions.add(genderDistribution);
            }
        }

        int flag1 = 0;
        int flag2 = 0;
        int flag3 = 0;
        int flag4 = 0;
        int flag5 = 0;
        Integer count1 = 0;
        Integer count2 = 0;
        Integer count3 = 0;
        Integer count4 = 0;
        Integer count5 = 0;

        for (String city : set3) {
            QueryWrapper<UserInfo> queryWrapper1 = new QueryWrapper<>();
            LocalDistribution localDistribution = new LocalDistribution();
            if(city.equals("黑龙")){
                city="黑龙江";
            }
            if(city.equals("内蒙")){
                city="内蒙古";
            }
            localDistribution.setTitle(city);
            queryWrapper1.gt("created", date1).lt("created", date2).like("city", city);
            localDistribution.setAmount(this.userInfoMapper.selectCount(queryWrapper1));

            localDistributions.add(localDistribution);

            if(CityEnum.E.getValue().contains(city)){
                flag1 = 1;
                Integer amount = localDistribution.getAmount();
                count1 += amount;
            }
            if(CityEnum.S.getValue().contains(city)){
                flag2 = 2;
                Integer amount = localDistribution.getAmount();
                count2 += amount;
            }
            if(CityEnum.W.getValue().contains(city)){
                flag3 = 3;
                Integer amount = localDistribution.getAmount();
                count3 += amount;
            }
            if(CityEnum.N.getValue().contains(city)){
                flag4 = 4;
                Integer amount = localDistribution.getAmount();
                count4 += amount;
            }
            if(CityEnum.Z.getValue().contains(city)){
                flag5 = 5;
                Integer amount = localDistribution.getAmount();
                count5 += amount;
            }

        }

        if(flag1 == 1){
            LocalTotal localTotal = new LocalTotal();
            localTotal.setTitle(CityEnum.E.toString());
            localTotal.setAmount(count1);
            localTotals.add(localTotal);
        }
        if(flag2 == 2){
            LocalTotal localTotal = new LocalTotal();
            localTotal.setTitle(CityEnum.S.toString());
            localTotal.setAmount(count2);
            localTotals.add(localTotal);
        }
        if(flag3 == 3){
            LocalTotal localTotal = new LocalTotal();
            localTotal.setTitle(CityEnum.W.toString());
            localTotal.setAmount(count3);
            localTotals.add(localTotal);
        }
        if(flag4 == 4){
            LocalTotal localTotal = new LocalTotal();
            localTotal.setTitle(CityEnum.N.toString());
            localTotal.setAmount(count4);
            localTotals.add(localTotal);
        }
        if(flag5 == 5){
            LocalTotal localTotal = new LocalTotal();
            localTotal.setTitle(CityEnum.Z.toString());
            localTotal.setAmount(count5);
            localTotals.add(localTotal);
        }

        distributionVo.setIndustryDistribution(industryDistributions);
        distributionVo.setLocalDistribution(localDistributions);
        distributionVo.setGenderDistribution(genderDistributions);
        distributionVo.setAgeDistribution(ageDistributions);
        distributionVo.setLocalTotal(localTotals);
        String value = MAPPER.writeValueAsString(distributionVo);
        System.out.println(value);

    }

    private void getAge1(Date date1, Date date2, List<AgeDistribution> ageDistributions) {
        QueryWrapper<UserInfo> queryWrapper1 = new QueryWrapper<>();
        AgeDistribution ageDistribution = new AgeDistribution();

        ageDistribution.setTitle("0-17岁");
        queryWrapper1.gt("created", date1).lt("created", date2).ge("age",0).le("age",17);
        ageDistribution.setAmount(this.userInfoMapper.selectCount(queryWrapper1));

        ageDistributions.add(ageDistribution);
    }

    private void getAge2(Date date1, Date date2, List<AgeDistribution> ageDistributions) {
        QueryWrapper<UserInfo> queryWrapper1 = new QueryWrapper<>();
        AgeDistribution ageDistribution = new AgeDistribution();

        ageDistribution.setTitle("18-23岁");
        queryWrapper1.gt("created", date1).lt("created", date2).ge("age",18).le("age",23);
        ageDistribution.setAmount(this.userInfoMapper.selectCount(queryWrapper1));

        ageDistributions.add(ageDistribution);
    }

    private void getAge3(Date date1, Date date2, List<AgeDistribution> ageDistributions) {
        QueryWrapper<UserInfo> queryWrapper1 = new QueryWrapper<>();
        AgeDistribution ageDistribution = new AgeDistribution();

        ageDistribution.setTitle("24-30岁");
        queryWrapper1.gt("created", date1).lt("created", date2).ge("age",24).le("age",30);
        ageDistribution.setAmount(this.userInfoMapper.selectCount(queryWrapper1));

        ageDistributions.add(ageDistribution);
    }

    private void getAge4(Date date1, Date date2, List<AgeDistribution> ageDistributions) {
        QueryWrapper<UserInfo> queryWrapper1 = new QueryWrapper<>();
        AgeDistribution ageDistribution = new AgeDistribution();

        ageDistribution.setTitle("31-40岁");
        queryWrapper1.gt("created", date1).lt("created", date2).ge("age",31).le("age",40);
        ageDistribution.setAmount(this.userInfoMapper.selectCount(queryWrapper1));

        ageDistributions.add(ageDistribution);
    }

    private void getAge5(Date date1, Date date2, List<AgeDistribution> ageDistributions) {
        QueryWrapper<UserInfo> queryWrapper1 = new QueryWrapper<>();
        AgeDistribution ageDistribution = new AgeDistribution();

        ageDistribution.setTitle("41-50岁");
        queryWrapper1.gt("created", date1).lt("created", date2).ge("age",41).le("age",50);
        ageDistribution.setAmount(this.userInfoMapper.selectCount(queryWrapper1));

        ageDistributions.add(ageDistribution);
    }

    private void getAge6(Date date1, Date date2, List<AgeDistribution> ageDistributions) {
        QueryWrapper<UserInfo> queryWrapper1 = new QueryWrapper<>();
        AgeDistribution ageDistribution = new AgeDistribution();

        ageDistribution.setTitle("50岁+");
        queryWrapper1.gt("created", date1).lt("created", date2).gt("age",50);
        ageDistribution.setAmount(this.userInfoMapper.selectCount(queryWrapper1));

        ageDistributions.add(ageDistribution);
    }

}